//
//  LZRestaurantAuthTextCell.m
//  laziz_Merchant
//
//  Created by xbnzd on 17/3/3.
//  Copyright © 2017年 XBN. All rights reserved.
//

#import "LZRestaurantAuthTextCell.h"
#import "LZRestaurantAuthModel.h"

@interface LZRestaurantAuthTextCell()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cellNameLabel;
@property (weak, nonatomic) IBOutlet UITextView *cellContentTextView;
@property (strong,nonatomic) NSIndexPath *indexPath;
@property (weak, nonatomic) IBOutlet UILabel *placeHolderLabel;

@end

@implementation LZRestaurantAuthTextCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.cellContentTextView.textContainerInset = UIEdgeInsetsMake(0,0, 0, 0);
    self.cellContentTextView.delegate = self;
    self.cellContentTextView.tintColor = UIColorFromRGB(0x36bb2a, 1);
    self.placeHolderLabel.backgroundColor = [UIColor clearColor];
}

#pragma mark -
#pragma mark data
- (void)loadTextCellWithIndexPath:(NSIndexPath *)indexP andModel:(LZRestaurantAuthModel *)authModel
{
    self.placeHolderLabel.hidden = NO;
    self.indexPath = indexP;
    self.cellContentTextView.keyboardType = UIKeyboardTypeDefault;
    if (indexP.section == 0) {
        switch (indexP.row) {
            case 0:{//餐厅名称
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_restaurant_name", nil);
                if (authModel.restaurantName.length == 0) {
                    self.placeHolderLabel.text = LZLocalizedString(@"res_auth_enter_your_restaurant_name", nil);
                    self.cellContentTextView.text = @"";
                    self.cellContentTextView.scrollEnabled = YES;
                }else{
                    self.placeHolderLabel.hidden = YES;
                    self.cellContentTextView.text = authModel.restaurantName;
                    self.cellContentTextView.scrollEnabled = NO;
                }
                
            }
                break;
            case 2:{//餐厅地址
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_restaurant_address", nil);
                if (authModel.restaurantAddress.length == 0) {
                    self.placeHolderLabel.text = LZLocalizedString(@"res_auth_enter_restaurant_addredd", nil);
                    self.cellContentTextView.text = @"";
                    self.cellContentTextView.scrollEnabled = YES;
                }else{
                    self.placeHolderLabel.hidden = YES;
                    self.cellContentTextView.text = authModel.restaurantAddress;
                    self.cellContentTextView.scrollEnabled = NO;
                }
            }
                break;
            case 6:{//餐厅电话
                self.cellNameLabel.text = LZLocalizedString(@"res_auth_phone", nil);
                self.cellContentTextView.keyboardType = UIKeyboardTypePhonePad;
                if (authModel.cellphone.length == 0) {
                    self.placeHolderLabel.text = LZLocalizedString(@"res_auth_enter_phone", nil);
                    self.cellContentTextView.text = @"";
                    self.cellContentTextView.scrollEnabled = YES;
                }else{
                    self.placeHolderLabel.hidden = YES;
                    self.cellContentTextView.text = authModel.cellphone;
                    self.cellContentTextView.scrollEnabled = NO;
                }
            }
                break;
                
            default:
                break;
        }
    }else if (indexP.section == 1){
        if (indexP.row == 0) {
            self.cellNameLabel.text = LZLocalizedString(@"res_auth_company_name", nil);
            if (authModel.companyName.length == 0) {
                self.placeHolderLabel.text = LZLocalizedString(@"res_auth_enter_company_name", nil);
                self.cellContentTextView.text = @"";
                self.cellContentTextView.scrollEnabled = YES;
            }else{
                self.placeHolderLabel.hidden = YES;
                self.cellContentTextView.text = authModel.companyName;
                self.cellContentTextView.scrollEnabled = NO;
            }
        }
    }
}


#pragma mark -
#pragma mark method
+ (CGFloat)getCellHeightWithText:(NSString *)text
{
    if (text.length > 0) {
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:15.]};
        CGRect titleRect = [text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH-20, MAXFLOAT)
                                              options:NSStringDrawingUsesLineFragmentOrigin
                                           attributes:attributes
                                              context:nil];
        return 20 + 20 + 15 + titleRect.size.height + 22;
    }
    else {
        return 20 + 20 + 15 + 20 + 15;
    }
}

- (void)changeCellTextViewEnabled:(BOOL)enable
{
    if (enable == NO) {
        self.cellContentTextView.userInteractionEnabled = NO;
    }else{
        self.cellContentTextView.userInteractionEnabled = YES;
    }
}

#pragma mark -
#pragma mark textview delegate
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSInteger totalLength = textView.text.length;//已经输入确认的
    NSInteger replaceLength = text.length; //键盘输入了,但是未确认的
//    NSInteger changeLength = range.length; //正在输入的
    
    if (totalLength < 1  && replaceLength < 1) {
        self.placeHolderLabel.hidden = NO;
    }else{
        self.placeHolderLabel.hidden = YES;
    }
    
    //字数限制
    if (totalLength + text.length > 50) {
        return NO;
    }
    
    return YES;
}
- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (self.restaurantAuthTextCellDelegate && [self.restaurantAuthTextCellDelegate respondsToSelector:@selector(returnTextViewContent:withIndexPath:)]) {
        [self.restaurantAuthTextCellDelegate returnTextViewContent:textView.text withIndexPath:self.indexPath];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}


@end
